package com.itmuch.filter;

import com.dianping.cat.Cat;
import com.itmuch.common.cat.CatMsgConstants;
import com.itmuch.common.cat.feign.FeignCat;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * zuul 的统一过滤器处理 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author Administrator
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class AccessFilter extends ZuulFilter {

	private final static Logger LOGGER = LoggerFactory.getLogger(AccessFilter.class);


	private static final String APP_ID = "X-AUTH-ID";
	private static final String APP_KEY = "X-AUTH-KEY";
	private static String[] interceptUrl = {"/login/addLoginSuccessMark","/login/addSSOLoginSuccessMark","/login/deleteUserLoginState","/authorizedApplication","/companyDomains/info","/lklBff/notifyOrder"};
	
	private static String[] swaggerUrlArr = { "/springfox-swagger-ui","/swagger-ui.html","/swagger-resources", "/v2/api-docs"};
	

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	private boolean checkAppArgNotNull(String appId, String appKey) {
		return !StringUtils.isEmpty(appId) && !StringUtils.isEmpty(appKey);
	}
	
	private boolean isContainFilterUrl(String[] filterArr,String uri){
		for (String swaggerUrl : swaggerUrlArr){
			if (!StringUtils.isEmpty(swaggerUrl) && uri.indexOf(swaggerUrl) > 0 ){
				  return true;
		    	}
		 }
	   return false;
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		HttpServletResponse httpResponse = ctx.getResponse();
		httpResponse.setCharacterEncoding("UTF-8");
		httpResponse.setContentType("application/json; charset=utf-8");
		String uri = request.getRequestURI();
		String token1  = request.getHeader("auth-token");
		LOGGER.debug("The uri=" + uri +",content token:"+token1);
		//默认信息量，不能使用hstrix的拦截器
		FeignCat.createMessageTree();
		RequestContext requestContext = RequestContext.getCurrentContext();
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		Object root = requestAttributes.getAttribute(Cat.Context.ROOT, 0);
		if(root!=null){
			String rootId = requestAttributes.getAttribute(Cat.Context.ROOT, 0).toString();
			String childId = requestAttributes.getAttribute(Cat.Context.CHILD, 0).toString();
			String parentId = requestAttributes.getAttribute(Cat.Context.PARENT, 0).toString();
			requestContext.addZuulRequestHeader(Cat.Context.ROOT, rootId);
			requestContext.addZuulRequestHeader(Cat.Context.PARENT, parentId);
			requestContext.addZuulRequestHeader(Cat.Context.CHILD, childId);
			requestContext.addZuulRequestHeader("X-CAT-TRACE-MODE","true");
			requestContext.addZuulRequestHeader(CatMsgConstants.APPLICATION_KEY, Cat.getManager().getDomain());
			LOGGER.info(" start feign : " + request.getMethod() + " model : rootId = " + rootId + " parentId = " + parentId + " childId = " + childId);
		}
		return  null;
	}
}
